$(function () {


    layui.use('form', function () {
        var form = layui.form;

        form.on('submit(login)', function (data) {

            console.log("登录提交的数据:",data.field);
            $.ajax({
                url: 'http://localhost:8080/back/login',//对应的是Java servlet里面的注解地址
                data:JSON.stringify(data.field),// 要传递给Java的数据，统一以对象的形式传递{}
                type: 'post',//对应的是servlet里面的方法
                contentType: 'application/json',
                dataType: 'json',//期待后端返回给前端的数据格式
                xhrFields:{withCredentials:true},
                crossDomain:true,//是否为跨域请求
                success: function (res,stutas,xhr) {// Java后台给前端的返回信息，res就是返回的结果


                    let responseHeader = xhr.getResponseHeader("username");

                    var allResponseHeaders = xhr.getAllResponseHeaders();
                    console.log("响应头：",allResponseHeaders)

                    console.log("响应头：",responseHeader)

                    localStorage.setItem("loginUser",responseHeader);

                    console.log("loginUser:",localStorage.getItem("loginUser"))

                    console.log("res:",res);
                    if(res.msg=="error"){
                        layer.msg("密码或者用户名错误")
                    }else if(res.data.msg == "code-error"){

                        layer.msg("验证码错误");
                    }else if(res.data.msg == "success"){
                        layer.msg("登录成功",function () {
                            console.log("后台返回数据",res)

                            /*//设置前端页面的session
                            console.log(res.data.data.username)
                            sessionStorage.setItem("username",res.data.data.username);
                            console.log("存入session")
                            var aa= sessionStorage.getItem("username")
                            console.log(aa)*/
                            // location.href = "http://localhost:63342/front_page/front/html/home.html"
                            // location.href = "http://localhost:63342/front_page/front/html/home.html"
                            // location.href = "http://localhost:63342/front_page/front/html/home.html"

                            location.href = "http://localhost:63342/front_page/html/index.html"



                        });
                    }
                },
                error: function (res) {
                    console.log("错误返回的数据:",res);
                    alert("后端返回值错误")
                }
            });
            return false;
        });

        form.verify({
            username:[/^[A-z]/,"名字必须以字母开头"],
            password:[/(.+){5,8}/,"密码长度5到8位"],
        });


    });
})

function changeCode() {
    var number = Math.random();
    $("#checkCode").attr("src","http://localhost:8080/back/code?a="+number);
}

function display(obj) {

    //当前确认输入框类型是密码
    if($("#password").attr("type") == "password"){
        $("#password").attr("type","text")
        $(obj).attr("src","../img/open.png")
    }
    //当前确认输入框类型是文本
    else {
        $("#password").attr("type","password")
        $(obj).attr("src","../img/close.png")
    }
}